//
//  ZZLightLineModel.m
//  ZZBlue
//
//  Created by evan on 2017/10/16.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZZLightLineModel.h"

@implementation ZZLightLineModel

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.lightValue = 0.5;
        self.pinLv = 0.5;
        self.tuoWeiLength = 0;
        self.colorZuHeShu = 5;
        self.dicrection = LightLineDirectionLeft;
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    
    [aCoder encodeObject:self.title forKey:@"title"];
    [aCoder encodeInteger:self.model forKey:@"model"];
    [aCoder encodeBool:self.isAnticlockwise forKey:@"isAnticlockwise"];
    
    [aCoder encodeInteger:self.colorNumber forKey:@"colorNumber"];
    [aCoder encodeFloat:self.tuoWeiLength forKey:@"tuoWeiLength"];
    [aCoder encodeInteger:self.colorZuHeShu forKey:@"colorZuHeShu"];
    [aCoder encodeFloat:self.pinLv forKey:@"pinLv"];
    [aCoder encodeFloat:self.lightValue forKey:@"lightValue"];
    [aCoder encodeObject:self.imagePath forKey:@"imagePath"];
    [aCoder encodeCGSize:self.imageSize forKey:@"imageSize"];
    [aCoder encodeBool:self.isSelect forKey:@"isSelect"];
    [aCoder encodeObject:self.colorsGroup forKey:@"colorsGroup"];
    [aCoder encodeInteger:self.dicrection forKey:@"dicrection"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.model = [aDecoder decodeIntegerForKey:@"model"];
        self.isAnticlockwise = [aDecoder decodeBoolForKey:@"isAnticlockwise"];
        self.dicrection = [aDecoder decodeIntegerForKey:@"dicrection"];
        self.colorNumber = [aDecoder decodeIntegerForKey:@"colorNumber"];
        self.tuoWeiLength = [aDecoder decodeFloatForKey:@"tuoWeiLength"];
        self.colorZuHeShu = [aDecoder decodeIntegerForKey:@"colorZuHeShu"];
        self.pinLv = [aDecoder decodeFloatForKey:@"pinLv"];
        self.lightValue = [aDecoder decodeFloatForKey:@"lightValue"];
        self.imagePath = [aDecoder decodeObjectForKey:@"imagePath"];
        self.imageSize = [aDecoder decodeCGSizeForKey:@"imageSize"];
        self.isSelect = [aDecoder decodeBoolForKey:@"isSelect"];
        self.colorsGroup = [aDecoder decodeObjectForKey:@"colorsGroup"];
        self.dicrection = [aDecoder decodeIntegerForKey:@"dicrection"];
    }
    return self;
}

- (void)equalToModel:(ZZLightLineModel *)model {
    
    self.title = model.title;
    self.model = model.model;
    self.isAnticlockwise = model.isAnticlockwise;
    self.imageSize = model.imageSize;
    self.colorNumber = model.colorNumber;
    self.tuoWeiLength = model.tuoWeiLength;
    self.colorZuHeShu =  model.colorZuHeShu;
    self.pinLv = model.pinLv;
    self.lightValue = model.lightValue;
    self.imagePath = model.imagePath;
    self.isSelect = model.isSelect;
    self.colorsGroup = model.colorsGroup;
    self.dicrection = model.dicrection;
    
    
}

- (BOOL)isEqualModel:(ZZLightLineModel *)model {
    
    if (self.dicrection != model.dicrection) {
        
        return NO;
    }
    if (self.tuoWeiLength != model.tuoWeiLength) {
        
        return NO;
    }
    
    if (self.colorZuHeShu != model.colorZuHeShu) {
        
        return NO;
    }
    
    if (![self.title isEqualToString:model.title]) {
        
        return NO;
    }
    
    if (model.model != self.model) {
        
        return NO;
    }
    
    if(self.isAnticlockwise != model.isAnticlockwise) {
        
        return NO;
    }
    
    if (self.pinLv != model.pinLv) {
        
        return NO;
    }
    
    if (self.colorNumber != model.colorNumber) {
        
        return NO;
    }
    
    if (self.lightValue != model.lightValue) {
        
        return NO;
    }
    
    if (self.colors.count != model.colors.count) {
        
        return NO;
    }
    
    if (![self.imagePath isEqualToString:model.imagePath]) {
        return NO;
    }
    
    for (NSInteger j = 0; j < model.colorsGroup.count; j++) {
        
        NSMutableArray *colors = model.colorsGroup[j];
        for (NSInteger index = 0; index< colors.count; index++) {
            
            ZZColorRGBA *rgba = colors[index];
            ZZColorRGBA *modelRGBA = self.colorsGroup[j][index];
            if (rgba.R != modelRGBA.R || rgba.G != modelRGBA.G || rgba.B != modelRGBA.B || rgba.A != modelRGBA.A) {
                
                return NO;
            }
        }
    }
    return YES;
}

// 重新画图
- (void)reloadImage {
    
    switch (self.model) {
        case LightLineModelLiuXing:
        {
            CALayer *layer = [ZZLightViewTool liuXinglightLayerForColors:[self nativeColors] layerFrame:CGRectMake(0, 0, 280, 10) tuoweiLength:self.tuoWeiLength LightType:self.dicrection];
            layer.backgroundColor = [UIColor grayColor].CGColor;
            layer.cornerRadius = 5;
            
            self.imagePath = [self imageForLayer:layer];
            
        }
            break;
        case LightLineModelDieJia:
        {
            CALayer *layer = [ZZLightViewTool liuXinglightLayerForColors:[self nativeColors] layerFrame:CGRectMake(0, 0, 280, 10) tuoweiLength:self.tuoWeiLength LightType:self.dicrection];
            layer.backgroundColor = [UIColor grayColor].CGColor;
            layer.cornerRadius = 5;
            
            self.imagePath = [self imageForLayer:layer];
        }
            break;
        case LightLineModelTiaoBian:
        {
            
            CALayer *layer2 = [ZZLightViewTool jianBianlightLayerForColors:[self nativeColors] layerFrame:CGRectMake(0, 0, 280, 10) LightType:self.dicrection];
            layer2.backgroundColor = [UIColor grayColor].CGColor;
            layer2.cornerRadius = 5;
            self.imagePath = [self imageForLayer:layer2];
        }
            break;
        case LightLineModelFugai:
        {
            
            CALayer *layer3 = [ZZLightViewTool fugailightLayerForColors:[self nativeColors] layerFrame:CGRectMake(0,0, 280, 10) LightType:self.dicrection];
            layer3.backgroundColor = [UIColor grayColor].CGColor;
            layer3.cornerRadius = 5;
            
            self.imagePath = [self imageForLayer:layer3];
        }
            break;
        case LightLineModelJianGe:
        {
            if (self.dicrection == LightLineDirectionCenter) {
                
                CALayer *layer4 = [ZZLightViewTool JianGeLayerForColors:[self nativeColors] layerFrame:CGRectMake(0, 0, 280, 10) LightType:self.dicrection];
                self.imagePath = [self imageForLayer:layer4];
            } else {
                
                
                CALayer *layer3 = [ZZLightViewTool fugailightLayerForColors:[self nativeColors] layerFrame:CGRectMake(0,0, 280, 10) LightType:self.dicrection];
                layer3.backgroundColor = [UIColor grayColor].CGColor;
                layer3.cornerRadius = 5;
                
                self.imagePath = [self imageForLayer:layer3];
            }
        }
            break;
            
            
        default:
            break;
    }
}

// 重新画图
- (void)reloadImageForIndex:(NSInteger)index {
    
    switch (self.model) {
        case LightLineModelLiuXing:
        {
            CALayer *layer = [ZZLightViewTool liuXinglightLayerForColors:[self nativeColors] layerFrame:CGRectMake(0, 0, 280, 10) tuoweiLength:self.tuoWeiLength LightType:self.dicrection];
            layer.backgroundColor = [UIColor grayColor].CGColor;
            layer.cornerRadius = 5;
            
            self.imagePath = [self imageForLayer:layer];
            
        }
            break;
        case LightLineModelDieJia:
        {
            CALayer *layer = [ZZLightViewTool liuXinglightLayerForColors:[self nativeColors] layerFrame:CGRectMake(0, 0, 280, 10) tuoweiLength:self.tuoWeiLength LightType:self.dicrection];
            layer.backgroundColor = [UIColor grayColor].CGColor;
            layer.cornerRadius = 5;
            
            self.imagePath = [self imageForLayer:layer];
        }
            break;
        case LightLineModelTiaoBian:
        {
            
            CALayer *layer2 = [ZZLightViewTool jianBianlightLayerForColors:[self nativeColorsForIndex:index] layerFrame:CGRectMake(0, 0, 280, 10) LightType:self.dicrection];
            layer2.backgroundColor = [UIColor grayColor].CGColor;
            layer2.cornerRadius = 5;
            self.imagePath = [self imageForLayer:layer2];
        }
            break;
        case LightLineModelFugai:
        {
            
            CALayer *layer3 = [ZZLightViewTool fugailightLayerForColors:[self nativeColorsForIndex:index] layerFrame:CGRectMake(0,0, 280, 10) LightType:self.dicrection];
            layer3.backgroundColor = [UIColor grayColor].CGColor;
            layer3.cornerRadius = 5;
            
            self.imagePath = [self imageForLayer:layer3];
        }
            break;
        case LightLineModelJianGe:
        {
            if (self.dicrection == LightLineDirectionCenter) {
                
                CALayer *layer4 = [ZZLightViewTool JianGeLayerForColors:[self nativeColors] layerFrame:CGRectMake(0, 0, 280, 10) LightType:self.dicrection];
                self.imagePath = [self imageForLayer:layer4];
            } else {
                
                
                CALayer *layer3 = [ZZLightViewTool fugailightLayerForColors:[self nativeColorsForIndex:index] layerFrame:CGRectMake(0,0, 280, 10) LightType:self.dicrection];
                layer3.backgroundColor = [UIColor grayColor].CGColor;
                layer3.cornerRadius = 5;
                
                self.imagePath = [self imageForLayer:layer3];
            }
        }
            break;
            
            
        default:
            break;
    }
}

- (NSArray *)nativeColorsForIndex:(NSInteger)index {
    
    NSMutableArray *mtbArr = [NSMutableArray array];
    for (ZZColorRGBA *rgba in self.colorsGroup[index]) {
        
        UIColor *color = [UIColor zColorWithRed:rgba.R green:rgba.G blue:rgba.B];
        [mtbArr addObject:color];
    }
    
    return [mtbArr copy];
}

#pragma mark - 渲染图形
- (NSString *)imageForLayer:(CALayer *)layer{
    
    UIGraphicsBeginImageContext(layer.bounds.size);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY-HH-ss-mm-SSSS";
    NSString *str = [formatter stringFromDate:date];
    NSInteger index = arc4random() % 10000;
    NSString *name = [NSString stringWithFormat:@"%@%ld.png",str,index];
    
    NSString *path_document = NSHomeDirectory();
    //设置一个图片的存储路径
    NSString *imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",name]];
    
    NSData *_data = UIImagePNGRepresentation(viewImage);
    if ([_data writeToFile:imagePath atomically:YES]) {
        
        NSLog(@"写入成功");
    }
    
    return name;
}



#pragma mark - set & get
- (NSMutableArray<ZZColorRGBA *> *)colors {
    
    return self.colorsGroup[0];
}

- (NSArray *)outPutColors {
    
    if (self.model == LightLineModelFugai) {
        
        return self.colorsGroup;
    }
    
    return [self nativeColors];
}

- (NSArray *)nativeColors {
    
    NSMutableArray *mtbArr = [NSMutableArray array];
    for (ZZColorRGBA *rgba in self.colors) {
        
        UIColor *color = [UIColor zColorWithRed:rgba.R green:rgba.G blue:rgba.B];
        [mtbArr addObject:color];
    }
    
    return [mtbArr copy];
}

- (NSMutableArray<NSMutableArray<ZZColorRGBA *> *> *)colorsGroup {
    
    if (!_colorsGroup || _colorsGroup.count == 0) {
        
        _colorsGroup = [NSMutableArray array];
        
    }
    return _colorsGroup;
}


@end

